﻿using System;
using System.Configuration;
using System.Reflection;
using System.Xml;
using Swirebev.PhotoDownload.Model;
using System.Windows.Forms;

namespace Swirebev.PhotoDownload.Common
{
	public class Settings
	{
		public string ApplicationId { private set; get; }
		public string AppKey { private set; get; }

        public string QiniuAccessKey { private set; get; }
        public string QiniuSecretKey { private set; get; }

        public string QiniuBuckets { private set; get; }

		/// <summary>
		/// 每个时间片下载照片张数
		/// </summary>
		public int DownloadPhotoQuantityOnce { private set; get; }
		/// <summary>
		/// 每个时间片暂停时间，单位毫秒
		/// </summary>
		public int DownloadSleep { private set; get; }
		/// <summary>
		/// 照片保存目录，活动检查图片
		/// </summary>
		public string PhotoSavePath { private set; get; }
		/// <summary>
		/// 照片保存目录，客户门招图片
		/// </summary>
		public string CustomerphotoSavePath { private set; get; }
		/// <summary>
		/// 照片保存目录，所有照片
		/// </summary>
		public string photoSaveAllPath { private set; get; }
		/// <summary>
		/// 数据库连接字符串
		/// </summary>
		public string ConnectionString { private set; get; }
        /// <summary>
        /// RR库数据连接
        /// </summary>
        public string connectionStringTime { private set; get; }
  


        public string ProxyScheme { private set; get; }
        public string ProxyUrl { private set; get; }
        public string ProxyUserName { private set; get; }
        public string ProxyPassword { private set; get; }
        public string ProxyPort { private set; get; }

        public int imageWidth { private set; get; }
        public int imageHeight { private set; get; }

        public long putTime { private set; get; }

        /// <summary>
        /// endTime
        /// </summary>
        public long endTime { private set; get; }
		/// <summary>
		/// 初始化
		/// </summary>
		/// <returns></returns>
		public bool Init()
		{
			try
			{
                //获取七牛云配置信息
                this.QiniuAccessKey = this.getConfig("QiniuAccessKey");
                this.QiniuSecretKey = this.getConfig("QiniuSecretKey");
                this.QiniuBuckets = this.getConfig("QiniuBuckets");

                //获取LeanCloud配置信息
				this.ApplicationId = this.getConfig("applicationId");
				this.AppKey = this.getConfig("appKey");

				this.DownloadPhotoQuantityOnce = Convert.ToInt32(this.getConfig("downloadPhotoQuantityOnce"),10);
				this.DownloadSleep = Convert.ToInt32(this.getConfig("downloadSleep"), 10) * 60 * 1000;
				this.photoSaveAllPath = this.getConfig("photoSaveAllPath");
				this.ConnectionString = this.getConfig("connectionString");
                this.connectionStringTime = this.getConfig("connectionStringTime");
				//this.destPath = this.getConfig("destPath");
                this.putTime = Convert.ToInt64(this.getConfig("putTime"));
                this.endTime = Convert.ToInt64(this.getConfig("endTime"));

                this.ProxyScheme = this.getConfig("ProxyScheme");
                this.ProxyUrl = this.getConfig("ProxyUrl");
                this.ProxyUserName = this.getConfig("ProxyUserName");
                this.ProxyPassword = this.getConfig("ProxyPassword");
                this.ProxyPort = this.getConfig("Proxyport");

                //this.imageWidth = this.getConfig("imageWidth");
                //this.imageHeight = this.getConfig("imageHeight");

				Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "success to read config");
				return true;
			}
			catch (Exception ex)
			{
				Log.Error(MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
				return false;
			}
		}

		private string getConfig(string key)
		{
			ConfigurationManager.RefreshSection("appSettings");
			string value = ConfigurationManager.AppSettings[key];//.Get(key);
			if (value == null)
				throw new Exception("Read [" + key + "] failed!");
			return value;
		}

        public static void setConfig(string name, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            config.AppSettings.Settings[name].Value = value;
            config.Save(ConfigurationSaveMode.Modified);
            config = null;

            
           
                           

        }



		private static Settings _instance;
		public static Settings Instance
		{
			get 
			{
				if (_instance == null)
				{
					_instance = new Settings();
				}
				return _instance;
			}
		}

	}
}
